package com.ruoyi.web.admin.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysAlertConfig;
import com.ruoyi.system.service.ISysAlertConfigService;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 预警项目配置 信息操作处理
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/system/alert/config")
public class SysAlertConfigController extends BaseController {
    private final ISysAlertConfigService sysAlertConfigService;

    public SysAlertConfigController(ISysAlertConfigService sysAlertConfigService) {
        this.sysAlertConfigService = sysAlertConfigService;
    }

    /**
     * 获取预警项目配置列表
     */
    @PreAuthorize("@ss.hasPermi('system:alert:config:list')")
    @GetMapping("/list")
    @ApiOperation("获取预警项目配置列表")
    public TableDataInfo list(SysAlertConfig sysAlertConfig) {
        startPage();
        List<SysAlertConfig> list = sysAlertConfigService.selectSysAlertConfigList(sysAlertConfig);
        return getDataTable(list);
    }

    /**
     * 根据预警项目配置ID获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:alert:config:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation("根据预警项目配置ID获取详细信息")
    public AjaxResult getInfo(@PathVariable Long id) {
        return success(sysAlertConfigService.selectSysAlertConfigById(id));
    }

    /**
     * 根据预警大项类别和预警级别获取配置
     */
    @PreAuthorize("@ss.hasPermi('system:alert:config:query')")
    @GetMapping(value = "/project/{projectCate}/level/{alertLevel}")
    @ApiOperation("根据预警大项类别和预警级别获取配置")
    public AjaxResult getByProjectAndLevel(@PathVariable String projectCate, @PathVariable String alertLevel) {
        return success(sysAlertConfigService.selectSysAlertConfigByProjectAndLevel(projectCate, alertLevel));
    }

    /**
     * 新增预警项目配置
     */
    @PreAuthorize("@ss.hasPermi('system:alert:config:add')")
    @Log(title = "预警项目配置", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增预警项目配置")
    public AjaxResult add(@Validated @RequestBody SysAlertConfig sysAlertConfig) {
        if (sysAlertConfigService.checkAlertConfigUnique(sysAlertConfig)) {
            return error("新增预警项目配置失败，预警项目配置已存在");
        }
        if (sysAlertConfig.getAlertName() == null || sysAlertConfig.getAlertName().isEmpty()) {
            sysAlertConfig.setAlertName(sysAlertConfig.getItemName());
        }
        return toAjax(sysAlertConfigService.insertSysAlertConfig(sysAlertConfig));
    }

    /**
     * 修改预警项目配置
     */
    @PreAuthorize("@ss.hasPermi('system:alert:config:edit')")
    @Log(title = "预警项目配置", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改预警项目配置")
    public AjaxResult edit(@Validated @RequestBody SysAlertConfig sysAlertConfig) {
        if (sysAlertConfigService.checkAlertConfigUnique(sysAlertConfig)) {
            return error("修改预警项目配置失败，预警项目配置已存在");
        }
        if (sysAlertConfig.getAlertName() == null || sysAlertConfig.getAlertName().isEmpty()) {
            sysAlertConfig.setAlertName(sysAlertConfig.getItemName());
        }
        return toAjax(sysAlertConfigService.updateSysAlertConfig(sysAlertConfig));
    }

    /**
     * 删除预警项目配置
     */
    @PreAuthorize("@ss.hasPermi('system:alert:config:remove')")
    @Log(title = "预警项目配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation("根据ID删除预警配置")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(sysAlertConfigService.deleteSysAlertConfigByIds(ids));
    }
}